/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_GB_END_USER_H
#define MK_GB_END_USER_H
#include <MkGb28181/MkGb28181.h>
#include <MkPs/MkPsStreamSource.h>

typedef struct _rtp_param
{
    BOOL        bTcp;
    MkString    Host;
    Uint16      Port;
    Uint32      Ssrc;
    Uint8       Payload;
}MkRtpParam;
typedef MkFunction<void(BOOL bOnline)> MkGbSvrConnectCallback;
typedef MkFunction<void(Uint32 ErrorCode, const MkString& SessionId, const MkRtpParam& AnswerRtpParam, const EventResponseCallback& EventCb)> MkStreamReslutCallback;
typedef MkFunction<void(const MkString& SessionId)> MkStreamStoppedCallback;
class MkApi CMkGbEndUser : public CMkGb28181
{
    typedef struct _end_user_play_param
    {
        MkString PlaySession;
        MkStreamStoppedCallback StoppedCb;
        MkStreamReslutCallback  ResultCb;
    }MkGbEndUserPlayParam;
public:
    CMkGbEndUser(CMkEventPool* pEventPool, CMkTimer* pTimer);
    ~CMkGbEndUser();

    Uint32 Start(const MkGbStartParam& Param);
    Uint32 SetRegisterServer(const MkString& ServerId, const MkString& ServerRealm, const MkString& ServerHost, Uint16 Port, BOOL bTcpSip, const MkString& UserName, const MkString& Password, Uint32 Expires, Uint32 HbInterval, Uint32 HbCount, const MkGbSvrConnectCallback& ConnectCb);

    Uint32 StartPlay(const MkString& SipUri, BOOL bTcpSip, Uint32 WndIndex, const MkRtpParam& OfferRtpParam, const MkStreamReslutCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb);
    Uint32 StartPlay(const MkString& ChannelId, const MkString& PlayType, const CMkTime& BeginTime, const CMkTime& EndTime, Uint32 WndIndex, const MkRtpParam& OfferRtpParam, const MkStreamReslutCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb);
    Uint32 StopPlay(const MkString& SessionId);

    //invite with sdp
    Uint32 StartPublish(const MkString& SipUri, BOOL bTcpSip, const MkRtpParam& OfferRtpParam, const MkStreamReslutCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb);
    //ack with sdp
    Uint32 StartPublish2(const MkString& SipUri, BOOL bTcpSip, const MkRtpParam& OfferRtpParam, const MkStreamReslutCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb);
    Uint32 StartPublish(const MkString& ChannelId, const MkRtpParam& OfferRtpParam, const MkStreamReslutCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb);
    Uint32 StopPublish(const MkString& SessionId);
    Uint32 ByeRequest(const MkString &PlaySession, const MkGbResponseCallback &Cb) override;
protected:
private:
    Uint32 OnGbRequest(const CMkGbRequest* pGbReq, const MkGbResponseCallback& Cb);
    Uint32 OnGbByeRequest(const CMkGbByeReqeust* pGbReq, const MkGbResponseCallback& Cb);
private:
    Uint32 DeletePlay(const MkString& SessionId);
private:
    Uint32 RegisterServer(const CMkGbDeviceAuthorRequest& Req, BOOL bFailEnd = FALSE);
    Uint32 KeepAliveServer();
private:
    MkString                m_RegServerId;
    MkString                m_RegServerRealm;
    MkString                m_RegHost;
    Uint16                  m_RegPort;
    BOOL                    m_RegTcpSip;
    MkString                m_RegServerUserName;
    MkString                m_RegServerPassword;
    Uint32                  m_RegExpires;
    Uint32                  m_HbInterval;
    Uint32                  m_HbCount;
    MkString                m_TimerCheckSession;
    Uint32                  m_RtcpTimeOutSec;
    Uint32                  m_RequestTimeOutSec;
    CMkTime                 m_LastActiveTime;
    MkString                m_KeepAliverTimer;
    MkString                m_RegisterDelayer;
    MkGbSvrConnectCallback  m_ConnectCb;
    CMkRwLock                                m_MediaSourceLock;
    MkMap<MkString, MkGbEndUserPlayParam>    m_MapMediaSource;
};

#endif